const express = require("express");
// const  App=express();
//导入操作server的数据模型
// const serverModel = require("../model/chatServerModel");
//引入数据库
const mysql2 = require("mysql");
const config = require("../config/config");

//实现本地链接
// console.log(config.host)
const connection = mysql2.createConnection({
    host: config.mysql.host,
    user: config.mysql.user,
    password: config.mysql.password,
    multipleStatements: true,
    database: config.mysql.database,
});

connection.connect((err) => {
    if (err) throw err;
    // console.log("DB数据库连接成功0");
});

// 第二中写入数据库
const mysqlWrite = (req, res) => {
    // const sql = `INSERT INTO  baojia_data SET ?`;
    // let writeData = { SID: 12223 };
    var sid = req._parsedUrl.query;
    const sql = `SELECT *FROM baojia_data WHERE sid ='${sid} '`;
    try {
        connection.query(sql, (err, result) => {
            if (err) {
                // console.log("Db数据库错误", err);
                res.json(err);
                // return err;
            } else {
                // console.log("DB请求成功 ");
                let json = {
                    info: result,
                    msg: "success",
                    code: 200,
                };
                res.json(json);
                // return json;
                // res.send("写入成功");
            }
        });
    } catch (error) {
        console.log(error);
    }
};

module.exports = {
    mysqlWrite,
    // ld_z2,
    // ldz2FristWrite
};